﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using LibCommon;
using RTCCore;

namespace LibRTC.Core.MessageMap.BuddyEvent
{
    public class BuddyStatusHandler : SimpleMessageHandler
    {
        private static ILogger LOGGER = LoggerFactory.GetLogger("BuddyStateHandler", null);

        private IBuddyStatusChange _statusChange;
         
        public BuddyStatusHandler() {
            _rtcEvent = RTC_EVENT.RTCE_BUDDY;
            _name = "BuddyStateHandler";
        }

        public void SetBuddyStatusChange(IBuddyStatusChange statusChange) {
            _statusChange = statusChange;
        }

        public override void Process(RTC_EVENT rtcEventType, object rtcEvent)
        {
            Debug.Assert(RTC_EVENT.RTCE_BUDDY == rtcEventType);
            Debug.Assert(null != rtcEvent);
            Debug.Assert(rtcEvent is IRTCBuddyEvent2);

            IRTCBuddyEvent2 buddyEvent2 = rtcEvent as IRTCBuddyEvent2;
            Debug.Assert(null != buddyEvent2);

            IRTCBuddy2 buddy2 = (IRTCBuddy2)buddyEvent2.Buddy;
            Debug.Assert(null != buddy2);

            LOGGER.LogDebug(buddy2.PresentityURI + " change status to "+buddy2.Status);

            if(null != _statusChange) {
                _statusChange.OnBuddyStatusChange(buddy2.PresentityURI, buddy2.Status);
            }
        }

    }
}
